// pages/seat/seat.js
const util = require('../../utils/util.js')
var api = require('../../config/api.js');
const {
  $Toast
} = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    autoVisible: false,
    actions1: [{
      text: '预约',
      }, {
        text: '举报',
      },
      {
        text: '取消',
      }
    ],
    actions: [{
      name: '取消',
    }],
    reA: {
      rsId: null,
      isDis: false,
    },
    reB: {
      rsId: null,
      isDis: false,
    },
    reC: {
      rsId: null,
      isDis: false,
    },
    seatList: [],
    tmpSeat: null,
    room: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    console.log(options)
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log(data)
      self.setData({
        room: data.data,
        currentTime: data.currentTime
      })
      self.updateSeat()
    })
  },
updateSeat(){
  util.request(api.SeatList + '/' + this.data.room.id, {}, 'POST').then(res => {
    console.log(res)
    // this.list = res.data
    this.setData({
      seatList: res
    })

  }).catch(err => {
    console.log(err)
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //页面滚动执行方式
  onPageScroll(event) {
    this.setData({
      scrollTop: event.scrollTop
    })
  },
  handleOpen() {
    this.setData({
      visible: true
    });
  },
  autoReserveOpen() {
    this.setData({
      autoVisible: true
    });
  },
  handleCancel() {
    this.setData({
      visible: false,
      autoVisible: false
    });
  },

  handleClick({
    detail
  }) {
    const index = detail.index + 1;
    console.log(this.tmpSeat)

    this.setData({
      visible: false,
      autoVisible: false,
    });
  },
  handleClick1({
    detail
  }) {
    let index = detail.index + 1;
    console.log(index)
    if (index == 1) {
      if (this.data.reA.isDis){
        wx.showToast({
          title: '该座位已被预约',
          icon: 'none',
          duration: 2000
        })
      }else{
        this.reserve()
      }
    } else if (index == 2) {
      if (!this.data.reA.isDis) {
        wx.showToast({
          title: '该座位无法举报',
          icon: 'none',
          duration: 2000
        })
      }else{
        this.report()
      }
    } else {
      this.setData({
        visible: false,
      });
    }

  },
  clickItem(option) {
    this.tmpSeat = option.currentTarget.dataset.item;
    console.log(this.tmpSeat)
    let timeList = this.tmpSeat.seatTimeDto;

    if (timeList != null) {
      this.setData({
        reA: {
          rsId: null,
          isDis: false
        },
        reB: {
          rsId: null,
          isDis: false
        },
        reC: {
          rsId: null,
          isDis: false
        },
      });
      for (let i = 0; i < timeList.length; i++) {
        console.log(timeList[i])
        if (timeList[i].timeInterval == 0 || this.tmpSeat.sjdA == 1) {
          this.setData({
            reA: {
              rsId: timeList[i].id,
              isDis: true
            }
          });
        }
        if (timeList[i].timeInterval == 1 || this.tmpSeat.sjdB == 1) {
          this.setData({
            reB: {
              rsId: timeList[i].id,
              isDis: true
            }
          });
        }
        if (timeList[i].timeInterval == 2 || this.tmpSeat.sjdC == 1) {
          this.setData({
            reC: {
              rsId: timeList[i].id,
              isDis: true
            }
          });
        }
      }
    }
    this.setData({
      visible: true
    });

  },
  reserve() {
console.log(this.data)
    util.request(api.Reserve, {
      seatId: this.tmpSeat.id,
      timeInterval: this.data.currentTime
    }, 'POST').then(res => {
      console.log(res)
      if (res.success) {
        this.updateSeat()
        this.setData({
          visible: false,
          autoVisible: false
        });
      }
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
      // this.list = res.data
    }).catch(err => {
      console.log(err)
    });
  },
  autoReserve(val) {
    console.log(this.data.room)

    util.request(api.AutoReserve, {
      roomId: this.data.room.id,
      timeInterval: val.currentTarget.dataset.time_interval
    }, 'POST').then(res => {
      console.log(res)
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
      // this.list = res.data
    }).catch(err => {
      console.log(err)
    });
  },
  report() {
    let self = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths

        wx.uploadFile({
          url: api.Upload, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'userToken': wx.getStorageSync('userToken')
          },
          formData: {
            'seatId': self.tmpSeat.id,
            'timeInterval': self.data.currentTime
          },
          success(res) {
            const data = res.data
            wx.showToast({
              title: res.data,
              icon: 'none',
              duration: 2000
            })
            console.log(res)
            //do something
          }
        })
      }
    })
  }
})